package jsmc.opendata.pratikanant.activity;


import java.util.List;
import jsmc.android.object.DialogButton;
import jsmc.opendata.nantes.exceptions.ApiReseauException;
import jsmc.opendata.nantes.modele.Itineraire;
import jsmc.opendata.nantes.util.NeoLog;
import jsmc.opendata.pratikanant.MainGDActivity;
import jsmc.opendata.pratikanant.R;
import jsmc.opendata.pratikanant.adapter.ListItineraires;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class TempsParcours extends MainGDActivity {
    /** Called when the activity is first created. */
	
	private ListView liste = null;
	public  List<Itineraire> p = null;
	final Handler mHandler = new Handler();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setActionBarContentView(R.layout.temps_parcours_list);
        
        Thread mSplashThread =  new Thread(){
            @Override
            public void run(){
            	
            	p = getItineraires();
                mHandler.post(mUpdateResults);
                
            }
        };
        
        mSplashThread.start();
        ShowProgress(getString(R.string.dialog_connexion));
		
    }
    
    
    private void ShowList(){
    	
    	if(p!=null){
        
        	ListItineraires adapter = new ListItineraires(this);
        	adapter.SetItineraire(p);
    		liste = (ListView)  findViewById(R.id.temps_parcours_list_view);
    		liste.setAdapter(adapter);
    		
    		liste.setOnItemClickListener( new OnItemClickListener() {
    			
    			public void  onItemClick  (AdapterView<?> parent, View v, int position, long id) {
    				
    				Itineraire itineraire = p.get(position);
    				NeoLog.v("LB ItineraireActivity","ShowList pos:"+position+" id:"+position+" nom:"+itineraire.getTemps());
    				
    				  
    			}

    		});
    		
        } else {
        	
        	DialogButton PositiveButton = new DialogButton("OK", true);
			DialogButton NegativeButton = null;
			this.CreateDialog("Erreur", "Impossible de se connecter", 
        			PositiveButton, 
        			NegativeButton);
        	
        }
    }
    
    
    final Runnable mUpdateResults = new Runnable() {
        public void run() {
        	
        	CloseProgress();
        	
            ShowList();
        }
    };
    
    private List<Itineraire> getItineraires(){
    	
    	List<Itineraire> p = null;
    	try {
			
			p = app.op.getTempsParcours();
			
		} catch (ApiReseauException e) {

			e.printStackTrace();
			NeoLog.v("OP TempsParcours","Impossible de lire les data: "+ e.getMessage());
			
		}
    	
    	return p;
    }
}